﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using feather.attr;
using System.Windows.Forms;
using HealthReport.core;

using Word = Microsoft.Office.Interop.Word;
using Excel = Microsoft.Office.Interop.Excel;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Collections;
using System.Data.Common;
using System.Data;
using System.IO;

namespace HealthReport.unit
{
    class FormActionUnit
    {
        [ActionMethod]
        public void showDialog(
            string type, 
            [InputParam("params")] IDictionary<string, object> args)
        {
            Form form = Activator.CreateInstance(Type.GetType(type)) as Form;
            form.ShowDialog();
        }

        [ActionMethod]
        public void messageBox(string text, string caption)
        {
            MessageBox.Show(text, caption);
        }

        [ActionMethod]
        public DataTable readExcel(string filePath)
        {
            object oMissing = System.Reflection.Missing.Value;
            Excel.Application oExcel = new Excel.ApplicationClass();
            Excel._Workbook oWorkbook = oExcel.Workbooks.Open(filePath,
                oMissing, oMissing, oMissing,
                oMissing, oMissing, oMissing,
                oMissing, oMissing, oMissing,
                oMissing, oMissing, oMissing,
                oMissing, oMissing);

            Excel.Worksheet oWorksheet = oWorkbook.Worksheets[1] as Excel.Worksheet;

            int iRowCount = oWorksheet.UsedRange.Rows.Count;
            int iColCount = oWorksheet.UsedRange.Columns.Count;

            DataTable result = new DataTable();

            for (int r = 1; r < iRowCount; r++)
            {
                if (r == 1)
                {
                    for (int c = 1; c < iColCount; c++)
                    {
                        Excel.Range range = oWorksheet.Cells[r, c] as Excel.Range;

                        string str = (range.Value2 == null) ? "" : range.Text as string;
                        result.Columns.Add(str);
                    }
                }
                else
                {
                    DataRow dataRow = result.NewRow();
                    for (int c = 1; c < iColCount; c++)
                    {
                        Excel.Range range = oWorksheet.Cells[r, c] as Excel.Range;

                        string str = (range.Value2 == null) ? "" : range.Text as string;
                        dataRow[c - 1] = str;
                    }
                    result.Rows.Add(dataRow);
                }
            }

            oExcel.Quit();
            return result;
        }

        [ActionMethod]
        public void writeExcel(string filePath, DataTable data)
        {
            object oMissing = System.Reflection.Missing.Value;
            Excel.Application oExcel = new Excel.ApplicationClass();
            oExcel.DisplayAlerts = false;
            Excel._Workbook oWorkbook = oExcel.Workbooks.Add(oMissing);
            
            Excel.Worksheet oWorksheet = oWorkbook.Worksheets[1] as Excel.Worksheet;


            DataTable dataTable = data;

            for (int i = 0; i < dataTable.Columns.Count; i++)
            {
                oWorksheet.Cells[1, i + 1] = dataTable.Columns[i].ColumnName;
            }

            for (int i = 0; i < dataTable.Rows.Count; i++)
            {
                DataRow dataRow = dataTable.Rows[i];
                for (int j = 0; j < dataRow.ItemArray.Length; j++)
                {
                    oWorksheet.Cells[i + 2, j + 1] = dataRow[j] as string;
                }
            }

            Directory.CreateDirectory(Path.GetDirectoryName(filePath));

            oWorkbook.SaveAs(filePath,
                oMissing, oMissing, oMissing, oMissing,
                oMissing, Excel.XlSaveAsAccessMode.xlNoChange, oMissing, oMissing,
                oMissing, oMissing, oMissing);
            oExcel.Quit();
        }

        [ActionMethod]
        public void runMacro(string tempFile, string newFile, string macroName, [InputParam("params")] IDictionary<string, object> args)
        {
            object oMissing = System.Reflection.Missing.Value;
            object tempFile1 = Environment.CurrentDirectory + tempFile;

            object arg1 = JsonConvert.SerializeObject(args);

            //string macroName = "SimpleInput";
            Word._Application oWord = null;
            try
            {
                oWord = new Word.ApplicationClass();
                oWord.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;
                Word._Document oDoc = oWord.Documents.Add(ref tempFile1, ref oMissing, ref oMissing, ref oMissing);

                oWord.Run(macroName,
                    ref arg1, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

                oDoc.Close(ref oMissing, ref oMissing, ref oMissing);
            }
            finally
            {
                oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
            }

        }
    }
}
